package com.glasses.business.http;


import com.alibaba.fastjson.JSON;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.entity.User;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Description
 * Author by wangHaitao(a758277560@gmail.com)
 * Created  on 2015/12/picture1.
 * Version picture1.0
 */
public class NetUtils {

    /**
     * pageSize
     */
    public static int PAGE_SIZE = 10;

    private static NetUtils single = null;

    // 静态工厂方法
    public static NetUtils getInstance() {
        if (single == null) {
            single = new NetUtils();
        }
        return single;
    }

    public void norPost(String url, Object object, final NetCallBack callback, final Class classType) {
        OkHttpUtils.getInstance().postJsonAnsy(url, JSON.toJSONString(object), callback, classType);
    }

    public void norPost(String url, Map<String, Object> map, final NetCallBack callback, final Class classType) {
        if (map == null) {
            map = new HashMap<String, Object>();
        }
        User user = MyApplication.getInstance().user;
        if (user != null) {
            map.put("userid", user.getUserId());
            map.put("userToken", user.getUserToken());
        }
        // map.put("deviceType", 0+""); // 拼接userid
        OkHttpUtils.getInstance().postJsonAnsy(url, map, callback, classType);
    }

    public void norPostUser(String url, Map<String, Object> map, final NetCallBack callback, final Class classType) {
        if (map == null) {
            map = new HashMap<String, Object>();
        }
        if (MyApplication.getInstance().isLogin()) {
            map.put("token", MyApplication.getInstance().user.getUserToken()); // 拼接userid
        }
        //map.put("deviceType", 0); // 拼接userid
        OkHttpUtils.getInstance().postJsonAnsy(url, map, callback, classType);
        //OkHttpUtils.getInstance().SetSessionOutCls(WelcomeActivity.class);
    }

    /**
     * code (string, optional):
     * 验证码：接口为获取验证码时可为空 ,
     * phone (string):
     * 手机号
     *
     * @param phone
     * @param code
     * @param callback
     * @param classType
     */
    public void getCaptcha(String phone, String code, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("phone", phone);
        params.put("code", code);
        norPost(APPURL.CAPTCHA, params, callback, classType);
    }

    /**
     * 登录
     *
     * @param phone
     * @param callback
     * @param classType
     */
    public void login(String phone, String code, String registID, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("phone", phone);
        params.put("code", code);
        params.put("registID", registID);
        norPost(APPURL.LOGIN, params, callback, classType);
    }

    /**
     * 微信登录
     *
     * @param openId
     * @param userAcount
     * @param callback
     * @param classType
     */
    public void wxLogin(String openId, String userAcount, String registID, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("openId", openId);
        params.put("userAcount", userAcount);
        params.put("registID", registID);
        norPost(APPURL.WX_LOGIN, params, callback, classType);
    }

    /**
     * 首页轮播图
     *
     * @param callback
     * @param classType
     */
    public void homeBanner(final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        norPost(APPURL.Home_Banner, params, callback, classType);
    }

    /**
     * 首页商品列表
     *
     * @param page
     * @param rows
     * @param callback
     * @param classType
     */
    public void homeProduct(int page, int rows, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        norPost(APPURL.Home_Product, params, callback, classType);
    }

    /**
     * 资讯列表
     *
     * @param page
     * @param rows
     * @param callback
     * @param classType
     */
    public void newList(int page, int rows, String keyword, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        params.put("keyword", keyword);
        norPost(APPURL.News_List, params, callback, classType);
    }

    /**
     * 展示验光地址列表
     *
     * @param keyWord
     * @param latitude
     * @param longitude
     * @param page
     * @param rows
     * @param type
     * @param callback
     * @param classType
     */
    public void checkAddrs(String keyWord, double latitude, double longitude, int page, int rows, int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("keyWord", keyWord);
        params.put("latitude", latitude);
        params.put("longitude", longitude);
        params.put("page", page);
        params.put("rows", rows);
        if (type != -1) {
            params.put("type", type);
        }
        norPost(APPURL.Check_Addrs, params, callback, classType);
    }

    /**
     * 获取验光点选项
     *
     * @param type
     * @param callback
     * @param classType
     */
    public void checkAttr(int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        norPost(APPURL.Check_Attr, params, callback, classType);
    }

    /**
     * 上传验光单
     *
     * @param content
     * @param type
     * @param callback
     * @param classType
     */
    public void checkMenu(String content, int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("content", content);
        params.put("type", type);
        norPost(APPURL.Check_Menu, params, callback, classType);
    }

    /**
     * 删除验光单
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void delCheckMenu(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Del_Check_Menu, params, callback, classType);
    }

    /**
     * 获取验光单列表
     *
     * @param page
     * @param rows
     * @param callback
     * @param classType
     */
    public void checkMenuList(int page, int rows, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        norPost(APPURL.Check_Menu_List, params, callback, classType);
    }

    /**
     * 获取商品筛选条件列表
     *
     * @param callback
     * @param classType
     */
    public void selectItems(final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        norPost(APPURL.Select_Items, params, callback, classType);
    }

    /**
     * 获取订单信息
     *
     * @param productId
     * @param type
     * @param callback
     * @param classType
     */
    public void orderInfo(int productId, int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (productId != -1) {
            params.put("productId", productId);
        }
        params.put("type", type);
        norPost(APPURL.Order_Info, params, callback, classType);
    }

    /**
     * 获取商品详情
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void productDetail(int id, int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("type", type);
        norPost(APPURL.Product_Detail, params, callback, classType);
    }

    /**
     * 获取商品属性
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void productAttribute(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Product_Attribute, params, callback, classType);
    }

    /**
     * 获取镜片列表
     *
     * @param page
     * @param rows
     * @param callback
     * @param classType
     */
    public void product1List(int page, int rows, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        norPost(APPURL.Product1_List, params, callback, classType);
    }

    /**
     * 获取镜架列表
     *
     * @param page
     * @param rows
     * @param select
     * @param sort
     * @param callback
     * @param classType
     */
    public void product0List(int page, int rows, String select, int sort, int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        params.put("select", select);
        if (sort != -1) {
            params.put("sort", sort);
        }
        if (type != -1) {
            params.put("type", type);
        }
        norPost(APPURL.Product0_List, params, callback, classType);
    }

    public void commitOrder(int activityProductId, int addressId, int checkMenuId, int product0Id, String product0Notes, int product1Id, int type, int userMyProduct0, int isUploadCheckMenu, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityProductId", activityProductId);
        params.put("addressId", addressId);
        params.put("checkMenuId", checkMenuId);
        params.put("product0Id", product0Id);
        params.put("product0Notes", product0Notes);
        params.put("product1Id", product1Id);
        params.put("type", type);
        params.put("userMyProduct0", userMyProduct0);
        params.put("isUploadCheckMenu", isUploadCheckMenu);
        norPost(APPURL.Commit_Order, params, callback, classType);
    }

    /**
     * 添加收货地址
     *
     * @param address
     * @param phone
     * @param receiver
     * @param callback
     * @param classType
     */
    public void addAddress(int id, String address, String detailAddress, String phone, String receiver, int status, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("address", address);
        params.put("detailAddress", detailAddress);
        params.put("phone", phone);
        params.put("receiver", receiver);
        params.put("status", status);
        norPost(APPURL.Add_Address, params, callback, classType);
    }

    /**
     * 收货地址列表
     *
     * @param page
     * @param rows
     * @param callback
     * @param classType
     */
    public void addressList(int page, int rows, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        norPost(APPURL.Address_List, params, callback, classType);
    }

    /**
     * 默认收货地址
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void defaultAddr(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Default_Addr, params, callback, classType);
    }

    /**
     * 删除收货地址
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void delAddress(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Del_Address, params, callback, classType);
    }

    /**
     * 意见反馈
     *
     * @param content
     * @param phone
     * @param callback
     * @param classType
     */
    public void feedBack(String content, String phone, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("content", content);
        params.put("phone", phone);
        norPost(APPURL.Feed_Back, params, callback, classType);
    }

    /**
     * 订单列表
     *
     * @param page
     * @param rows
     * @param status
     * @param callback
     * @param classType
     */
    public void orderList(int page, int rows, int status, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        params.put("status", status);
        norPost(APPURL.Order_List, params, callback, classType);
    }

    /**
     * 获取订单详情
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void orderDetail(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Order_Detail, params, callback, classType);
    }

    /**
     * 确认收货
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void sureReceive(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Sure_Receive, params, callback, classType);
    }

    /**
     * 取消订单
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void cancelOrder(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Cancel_Order, params, callback, classType);
    }

    /**
     * 获取消息列表
     *
     * @param callback
     * @param classType
     */
    public void msgList(int page, int rows, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("rows", rows);
        norPost(APPURL.Msg_List, params, callback, classType);
    }

    /**
     * 获取消息详情
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void msgDetail(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Msg_Detail, params, callback, classType);
    }

    /**
     * 获取系统字段
     *
     * @param type
     * @param callback
     * @param classType
     */
    public void systemContent(int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        norPost(APPURL.System_Content, params, callback, classType);
    }

    /**
     * 检查更新
     *
     * @param callback
     * @param classType
     */
    public void updateVersion(final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        norPost(APPURL.Update_Version, params, callback, classType);
    }

    /**
     * 获取订单支付信息
     *
     * @param id
     * @param callback
     * @param classType
     */
    public void payInformation(int id, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        norPost(APPURL.Pay_Information, params, callback, classType);
    }

    /**
     * 售后
     *
     * @param callback
     * @param classType
     */
    public void backMoney(int id, String postCompany, String postNumber, int type, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("postCompany", postCompany);
        params.put("postNumber", postNumber);
        params.put("type", type);
        norPost(APPURL.Back_Money, params, callback, classType);
    }

}
